#include "contactname.h"
#include "ui_contactname.h"
#include <QDebug>
#include <QMouseEvent>



contactName::contactName(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::contactName)
{
    ui->setupUi(this);
    ui->CheckMark->setPixmap(QPixmap(":/uncheck.png").scaledToHeight(40,Qt::SmoothTransformation));
    mPresent=false;
    mAbsent=false;

}

contactName::~contactName()
{
    delete ui;
}

void contactName::setIndex(int aIndex)
{
    mIdex=aIndex;
}

void contactName::setName(QString aText)
{
    ui->name_field->setText(aText);
}

void contactName::setInfo(QString aText)
{
    //b ui->info_field->setText(aText);
}


void contactName::setImage(QPixmap aPixmap)
{
    ui->image->setPixmap(aPixmap);
}

void contactName::setUid(QByteArray aUID)
{
    muID=aUID;
}


void contactName::mousePressedTimeout()
{
    m_mousepressed_timer.stop();
    if(m_is_mouse_pressed && !mMouseMoved)  {
        qDebug() << "element::mousePressedTimeout() - emit longPress() signal";
        emit longPress(mIdex);
    }

}

void contactName::setPresent()
{
    mPresent=true;
    ui->CheckMark->setPixmap(QPixmap(":/check.png").scaledToHeight(40,Qt::SmoothTransformation));
}

QString contactName::getName()
{
    return ui->name_field->text();
}

QByteArray contactName::getUID()
{
    return muID;
}

bool contactName::event(QEvent *e)
{
    const QEvent::Type eventType = e->type();

    if (eventType==QEvent::MouseButtonPress)
    {
       QFeedbackEffect::playThemeEffect(QFeedbackEffect::ThemeBasicButton);

       ui->frame->setStyleSheet(ItemPressed);
        repaint();
        qDebug()<<"MousePressed";
        mMousePressed=true;
        mStart= QCursor::pos();
        qDebug()<<mStart;
        if(!m_is_mouse_pressed) {
            m_mousepressed_timer.start(1000);
            m_is_mouse_pressed = true;
        }

        //        QPalette p = palette();
        //        p.setColor( QPalette::Window, QColor(0,0,255) );
        //        setPalette(p);
    }
    else if((eventType==QEvent::MouseMove)&&(!mMouseMoved))
    {
        QMouseEvent* const mouseEvent = static_cast<QMouseEvent*>(e);
        QPoint mMouseMove = QCursor::pos();

        if((mStart.y()-mMouseMove.y()>10)||(mMouseMove.y()-mStart.y()>10))
        {
            //setStyleSheet(style);

            mMouseMoved=true;
        }
    }
    else if (eventType==QEvent::MouseButtonRelease)
    {
        if(mAbsent==false)
        ui->frame->setStyleSheet(ItemReleased);
        else
            ui->frame->setStyleSheet(ItemAbsent);
        //        QPalette p = palette();
        //        p.setColor( QPalette::Window, QColor(255,0,255) );
        //        setPalette(p);
        m_is_mouse_pressed = false;
        if (mMouseMoved)

            qDebug()<<"Do not emit signal";
        else
        {
            if ((mPresent==false)&&(mAbsent==false)){
                QFeedbackEffect::playThemeEffect(QFeedbackEffect::ThemeBasicButton);
                qDebug()<<"Emit a signal";
                emit shortPress(mIdex);
                emit studentClicked(muID,ui->name_field->text());
            }
        }
        mMouseMoved=false;
        mMousePressed=false;
        m_mousepressed_timer.stop();
        //setStyleSheet(style);
    }
    else if (eventType==QEvent::Polish)
    {
        //setStyleSheet(st);
    }

    return false;
}

void contactName::setAbsent()
{
mAbsent=true;
    ui->frame->setStyleSheet(ItemAbsent);
}


